<?php
require __DIR__ . '/../bootstrap.php';
require_once __DIR__ ."/../auth.php";

use Service\UserManager;

$em = require __DIR__ . '/../example/em.php';
//print_r($_POST);

if ($_POST['name']=='password') {
    $_POST['value']=md5($_POST['value']);
}
if ($_POST['name']=='group_user'){
    $_POST['name']='group';
}

$id=$_POST['pk'];
$target=$_POST['name'];
$manager = new UserManager($em);

if ($_POST['name']=='group' and $_POST['value']==2){
    $data['user_parent']=1;
}
$data = array(
    $target => $_POST['value'],
);
$user = $manager->update($data, $id);

//$data['user_parent']
//print_r($_POST);
//print_r($target);
print_r($data);

//file_put_contents('../test.log', $_POST['sum'], FILE_APPEND);
//exit();
if (isset($_POST['phone_number'])) {
    $phone_number=$_POST['phone_number'];
    check($phone_number, $method);
}
$method='inc';
//https://service.smart-sim.ru:443/api/37282879541/balance?uname=paul&upass=kam9345t
//https://service.smart-sim.ru:443/api/37282879541/inc?uname=paul&upass=kam9345t&amount=0.5&cur=EUR&trid=4565465464654
function check($phone_number, $method)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://service.smart-sim.ru:443/api/".$phone_number."/".$method."?uname=paul&upass=kam9345t&amount=".$_POST['sum']."&cur=EUR&trid=".mt_rand());
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 3);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "");
//    curl_setopt($ch, CURLOPT_INTERFACE, "192.168.1.104");
    curl_setopt($ch, CURLOPT_INTERFACE, "88.196.96.108");

    $result = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);

    //echo '<pre>';
    //print_r($info);
    //print_r($result);
    //echo '</pre>';

    if ($info['http_code'] != 200) {
        echo "HTTP: " . $info['http_code'];
    }
    $xml = simplexml_load_string($result);
    //return $xml;
    //echo '<pre>';
    //print_r($xml);
    //echo '</pre>';
}
?>